<?php

namespace App\model;

use App\Broker;
use App\Park;
use App\Website;
use App\WorkshopIndustry;
use App\WorkshopPropertyType;
use Composer\Cache;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

/**
 * App\model\Workshop
 *
 * @property int $id
 * @property string|null $title 厂房标题
 * @property int|null $rent 租金 单位25元/㎡/月
 * @property int|null $price 价格  5000元/平方米
 * @property int|null $type 类型：0为出租，1为出售
 * @property int|null $workshop_property_type_id 物业类型id
 * @property int|null $area 建筑面积 平方米
 * @property int|null $area_id 面积所属id
 * @property string|null $tag 标签 逗号分隔
 * @property string|null $address 厂房地址
 * @property int|null $built_area 建筑面积 平方米
 * @property float|null $high 首层层高
 * @property int|null $floor 楼层
 * @property float|null $elevator 电梯类型 T
 * @property float|null $distribution 配电类型 kv
 * @property string|null $code 厂房编号
 * @property string|null $owndesc 户主描述
 * @property int|null $park_id 园区id
 * @property int|null $workshop_industry_id 厂房行业id
 * @property string|null $ownertel 业主电话
 * @property string|null $owner 业主名称
 * @property string|null $slider 轮播图
 * @property string|null $desc 厂房描述
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $logo
 * @property string|null $latitude 经度
 * @property string|null $longitude 维度
 * @property-read \App\WorkshopIndustry|null $industry
 * @property-read \App\Park|null $park
 * @property-read \App\WorkshopPropertyType|null $propertyType
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereBuiltArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereDistribution($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereElevator($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereFloor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereHigh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereOwndesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereOwner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereOwnertel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereParkId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereWorkshopIndustryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereWorkshopPropertyTypeId($value)
 * @mixin \Eloquent
 * @property-read mixed $silder
 * @property string|null $p_province_id
 * @property string|null $p_city_id
 * @property string|null $p_district_id
 * @property string|null $payratio
 * @property string|null $matching_id
 * @property string|null $property
 * @property int|null $house_type
 * @property int|null $is_recommend 是否推荐
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereHouseType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereMatchingId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop wherePProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop wherePayratio($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereProperty($value)
 * @property int|null $click_count 点击量
 * @property int $call_count 电话次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereCallCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereClickCount($value)
 * @property-read \App\Broker|null $broker
 * @property string|null $is_out
 * @property string|null $owner_release
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereIsOut($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereOwnerRelease($value)
 */
class Workshop extends Model
{
    protected $table="workshop";
    protected $appends=['industry'];
    public function getLogoAttribute($value)
    {
        if($value){
            if(strstr($value,'http')){
                return $value.'?imageView2/0/w/512/h/384';
            }else{
               return Storage::url($value).'?imageView2/0/w/512/h/384';
            }
        }
        $silder= explode(',',$this->attributes['slider']);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(!preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
              return   $value.'?imageView2/0/w/512/h/384';
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
    }


    public function getTagAttribute($value){
        return explode(',',$value);
    }
    public function getSliderAttribute($value){
        $silder= explode(',',$value);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                $map['video'][]=$v;
            }else{
                $webSite = Website::find(1);
                $url = config('admin.app_outurl1').'/'.$webSite->watermark.'?imageView2/2/w/360/h/250';
                $map['image'][]=$v.'?imageView2/2/w/960/h/720|watermark/1/image/'.\Qiniu\base64_urlSafeEncode($url).'/gravity/'.$webSite->watermark_position;
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
        return $map;
    }

    public function getIndustryAttribute(){
       $industry= \Illuminate\Support\Facades\Cache::remember('industry','3',function(){
            return WorkshopIndustry::pluck('name','id')->toArray();
        });
       $option=$this->attributes['workshop_industry_id'];

       $array=[];
       $option=explode(',',$option);
           foreach ($option as $v){
               if(isset($industry[$v])){
                   $array[]=$industry[$v];
               }
           }
       return  $array;
    }
    public function propertyType(){
        return $this->belongsTo(WorkshopPropertyType::class,'workshop_property_type_id');
    }
    public function park(){
        return $this->belongsTo(\App\model\Park::class,'park_id');
    }
    public function broker(){
        return $this->belongsTo(\App\model\Broker::class,'owner');
    }
}
